package com.example.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.VO.ArticleLikeVO;
import com.example.demo.result.MyPage;
import com.example.demo.result.Result;
import com.example.demo.service.IArticleService;
import com.example.demo.service.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import com.example.demo.service.IArticleLikeService;
import com.example.demo.entity.ArticleLike;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 文章点赞表 前端控制器
 * </p>
 *
 * @author Mr.sdy
 * @since 2024-06-05
 */
@Api(tags = "文章点赞接口")
@RestController
@RequestMapping("/article-like")
public class ArticleLikeController {

    @Resource
    private IArticleLikeService articleLikeService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IArticleService articleService;

    /**
     * 查询所有接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "查询文章点赞表所有数据")
    @GetMapping
    public Result<List<ArticleLike>> findAll() {
        return Result.success(articleLikeService.selectAllLikes()).msg("查询成功");
    }

    /**
     * 查询指定文章点赞总数接口
     * @return
     */

    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "查询指定文章点赞总数接口")
    @GetMapping("/count1")
    public Result<Integer> count1(Integer articleId) {
        if(articleService.findArticleById(articleId) == null){
            return Result.error().msg("查询失败,文章不存在");
        }
        return Result.success(articleLikeService.countLikesByArticle(articleId)).msg("查询成功");
    }
    /**
     * 查询用户相关文章是否点赞接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "查询用户相关文章点赞接口")
    @GetMapping("/findLikesByUserAndArticle")
    public Result<Integer> findLikesByUserAndArticle(@Valid ArticleLikeVO articleLikeVO) {
        ArticleLike articleLike = new ArticleLike();
        BeanUtil.copyProperties(articleLikeVO,articleLike);
        if(userService.getById(articleLike.getUserId()) == null){
            return Result.error().msg("查询失败,用户不存在");
        }
        if(articleService.findArticleById(articleLike.getArticleId()) == null){
            return Result.error().msg("查询失败,文章不存在");
        }
        List<ArticleLike> articleLikes = articleLikeService.selectLikesByUserAndArticle(articleLike);
        return Result.success(articleLikes).msg("查询成功");
    }

    /**
    * 添加点赞
    * @param
    * @return
    */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "添加点赞")
    @PostMapping
    public Result<String> add(@RequestBody ArticleLikeVO articleLikeVO) {
        ArticleLike articleLike = new ArticleLike();
        BeanUtil.copyProperties(articleLikeVO,articleLike);
        if (userService.getById(articleLike.getUserId()) == null || articleService.findArticleById(articleLike.getArticleId()) == null){
            return Result.error().code(99403).msg("点赞失败,没有该用户或是文章");
        }
        if (articleLikeService.selectLikesByUserAndArticle(articleLike).size() > 0) {
            return Result.error().code(99403).msg("点赞失败,该用户已经点赞");
        }
        return Result.success(articleLikeService.addLike(articleLike)).msg("添加成功");
    }

    /**
     * 删除接口
     * @param articleLikeVO
     * @return
     */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "删除点赞接口")
    @DeleteMapping("/delete")
    public Result<String> delete(@RequestBody ArticleLikeVO articleLikeVO) {
        ArticleLike articleLike = new ArticleLike();
        BeanUtil.copyProperties(articleLikeVO,articleLike);
        if (userService.getById(articleLike.getUserId()) == null || articleService.findArticleById(articleLike.getArticleId()) == null){
            return Result.error().code(99403).msg("删除失败,没有该用户或是文章");
        }
        return Result.success(articleLikeService.deleteLike(articleLike)).msg("删除成功");
    }
}